package com.zhu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhu.bean.*;
import com.zhu.service.RentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RentController {
    @Autowired
    private RentService rentService;
    @RequestMapping("/page/publishRent.do")
    public ModelAndView publishRent(Rent rent,String districtId,String streetId,String typeId){
        ModelAndView mv = new ModelAndView();
        District district = new District();
        Street street = new Street();
        Type type = new Type();
        type.setTypeId(typeId);
        district.setDistrictId(Integer.parseInt(districtId));
        street.setStreetId(Integer.parseInt(streetId));
        rent.setDistrict(district);
        rent.setStreet(street);
        rent.setType(type);
        int i = rentService.insertRent(rent);
        if (i >0){
            mv.setViewName("redirect:/page/list.jsp");
            return mv;
        }
        mv.setViewName("redirect:error.html");
        return mv;
    }
    @RequestMapping("/page/list.do")
    @ResponseBody
    public Map<String, Object> getRentList(int pageIndex){
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(pageIndex, 4);
        List<Rent> rentList = rentService.getAll();
        PageInfo<Rent> pageInfo = new PageInfo<>(rentList,3);
        List<Rent> list = pageInfo.getList();
        if (rentList.size() >0){
            map.put("rentList",list);
            map.put("pageInfo",pageInfo);
            return map;
        }
        return null;
    }
    @RequestMapping("/page/detail.do")
    public ModelAndView detailrent(Integer id){
        ModelAndView mv = new ModelAndView();
        Rent rent = rentService.getById(id);
        if (rent != null){
            mv.addObject("rent",rent);
            mv.setViewName("forward:/page/detail.jsp");
            return mv;
        }
        mv.setViewName("redirect:rent/error.html");
        return mv;
    }
    @RequestMapping("/page/getByName.do")
    public ModelAndView detailrent(String nickName){
        ModelAndView mv = new ModelAndView();
        List<Rent> list = rentService.getByName(nickName);
        if (list.size()>0){
            mv.addObject("list",list);
            mv.setViewName("forward:/page/guanli.jsp");
            return mv;
        }
        mv.setViewName("redirect:error.html");
        return mv;
    }
   @RequestMapping("/page/delById.do")
   public ModelAndView delRent(int id ){
        ModelAndView mv =new ModelAndView();
       int i = rentService.delById(id);
       if (i > 0){
           mv.setViewName("forward:/page/getByName.do");
           return mv;
       }
       mv.setViewName("redirect:rent/error.html");
       return mv;
   }

    @RequestMapping(value = "/page/choose.do")
    @ResponseBody
    public List<Rent> getByChoose(Rent rent,String streetId,String typeId){
        int i = Integer.parseInt(streetId);
        rent.setStreet(new Street(i));
        rent.setType(new Type(typeId));
        List<Rent> chooseList = rentService.getByChoose(rent);
        if (chooseList.size()>0){
            return chooseList;
        }
       return null;
    }

}
